package com.cherish.meeting.common.http;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.Set;

/**
 * @author: wuhong-016
 * @time: 2021/3/11 16:52
 * @version: v1.0.0
 * @description:TODO
 */
@Slf4j
@Component
public class HttpComponent {

    @Resource
    private CloseableHttpClient httpClient;

    @Resource
    private RequestConfig requestConfig;


    /**
     * 发送httpGet请求
     *
     * @param url
     * @return
     */
    public String doGet(String url) {
        String result = null;
        log.info("HTTP GET url : {}", url);
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            int httpCode = response.getStatusLine().getStatusCode();
            log.info("HTTP GET code: {}", httpCode);
            if (httpCode == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
                log.info("HTTP GET result: {}", result);
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    /**
     * 发送httpPost请求
     *
     * @return
     */
    public String doPost(String url, Map<String, Object> paramMap) {
        String result = null;

        HttpPost httpPost = new HttpPost(url);

        List<NameValuePair> paramList = new ArrayList<>();

        if (paramMap == null) {//抛出异常

        } else {
            Set<String> keySet = paramMap.keySet();
            NameValuePair pair = null;
            for (String key : keySet) {
                pair = new BasicNameValuePair(key, paramMap.get(key).toString());
                paramList.add(pair);

            }

        }

        UrlEncodedFormEntity entity = null;
        CloseableHttpResponse response = null;

        try {
            entity = new UrlEncodedFormEntity(paramList);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");

            } else {


            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        } catch (Exception e1) {
            e1.printStackTrace();

        }

        return result;

    }

}
